package com.z.blog.controller;

import com.github.pagehelper.PageInfo;
import com.z.base.vo.PageVo;
import com.z.base.web.BaseController;
import com.z.blog.entity.Blog;
import com.z.blog.service.IBlogService;
import com.z.blog.service.ITypeService;
import com.z.utils.ImageBase64Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 博客标签Controller
 *
 * @author z
 * @date 2022-08-31
 */
@Controller
@RequestMapping("/blog/tag")
public class TagController extends BaseController {


    @Autowired
    private IBlogService blogService;
    @Autowired
    private ITypeService typeService;

    @GetMapping("/{id}")
    public String articlePage(@PathVariable Long id, PageVo pageVo, Model model) {
        PageInfo<Blog> pageInfo = pageInfo = blogService.selectBlogPageByTagId(id, pageVo);
        pageInfo.getList().forEach(o -> o.setUrl(ImageBase64Converter.convertFileToBase64(o.getFirstPicture())));

        model.addAttribute("page", pageInfo);
        model.addAttribute("types", typeService.listTypeTop(5));
        model.addAttribute("hotBlogs", blogService.listHotBlogTop(5));
        model.addAttribute("recommendBlogs", blogService.listRecommendBlogTop(5));
        return "blog/article";
    }
}
